#!/bin/sh
#
# Plugin to monitor solar panels
#
# Usage: Place in /etc/munin/node.d/ (or link it there  using ln -s)
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Magic markers - optional - used by installation scripts and
# munin-config:
#
#%# family=auto
#%# capabilities=autoconf

#read owfs config, then export it so backticks work
. /etc/default/owfs
export CLIENT_OPTS

if [ "$1" = "autoconf" ]; then
	if owdir $CLIENT_OPTS ; then
		echo yes
		exit 0
	else
		echo no
		exit 1
	fi
fi


if [ "$1" = "config" ]; then

	MAX="126"
	MIN="-2"
#	if [ "$scaleto100" = "yes" ]; then
#		graphlimit=100
#	else
#		graphlimit=$PERCENT
#	fi
#	WARNING="80"
#	CRITICAL="95"
	echo 'graph_title Solar Panel Temperatures'
	echo "graph_order panelin panel1 panel2 pump"
#	echo "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit"
	echo 'graph_vlabel Degrees Celcius'
#	echo 'graph_scale no'
	echo 'graph_info This graph shows solar panel temperatures.'
#	echo 'graph_category system'
	echo 'graph_period minute'
	echo 'panelin.label panels-in'
	echo 'panelin.draw LINE1'
	echo "panelin.max $MAX"
	echo "panelin.min $MIN"
	echo 'panelin.type GAUGE'
	echo "panelin.info Temp flow into solar panels"
	echo 'panel1.label panels-centre'
	echo 'panel1.draw LINE1'
	echo "panel1.min $MIN"
	echo "panel1.max $MAX"
	echo 'panel1.type GAUGE'
	echo 'panel1.info Temp 1st solar panel'
	echo 'panel2.label panels-out'
	echo 'panel2.draw LINE2'
	echo "panel2.min $MIN"
	echo "panel2.max $MAX"
	echo 'panel2.type GAUGE'
	echo 'panel2.info Temp 2nd solar panel'
	echo 'pump.label pump-running'
	echo 'pump.draw LINE'
#	echo 'pump.color black'
#	echo "pump.min 0"
#	echo "pump.max 1"
	echo 'pump.type GAUGE'
	echo 'pump.info Solar pump runnning'

#if [ "$scaleto100" = "yes" ]; then
#	echo "system.cdef system,$NCPU,/"
#	echo "user.cdef user,$NCPU,/"
#	echo "nice.cdef nice,$NCPU,/"
#	echo "idle.cdef idle,$NCPU,/"
#fi
	exit 0
fi


SENSOR1="10.73288A010800"
SENSOR2="10.B8E089010800"
SENSOR3="28.D23974010000"

PANELIN=`owread $CLIENT_OPTS $SENSOR1/temperature`
PANEL1=`owread $CLIENT_OPTS $SENSOR2/temperature`
PANEL2=`owread $CLIENT_OPTS $SENSOR3/fasttemp`
PANEL2=`echo "$PANEL2*1.1" | bc`
PUMP=`cat /sys/bus/i2c/devices/0-0038/read`
if [ $PUMP = 255 ]; then PUMP=0; else PUMP=5; fi

	echo "panelin.value $PANELIN"
	echo "panel1.value $PANEL1"
	echo "panel2.value $PANEL2"
	echo "pump.value $PUMP"

